package com.oig.sys.msg.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.oig.sys.msg.constants.MsgEnums;
import com.oig.sys.msg.dao.ITbTaskLogDao;
import com.oig.sys.msg.dto.TaskLogDto;
import com.oig.sys.msg.entity.TbTask;
import com.oig.sys.msg.entity.TbTaskLog;
import com.oig.sys.msg.service.ITbTaskLogService;
import com.oig.sys.msg.vo.TaskLogVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author wuxl
 */
@Service
public class TbTaskLogServiceImpl implements ITbTaskLogService {

	private final ITbTaskLogDao tbTaskLogDao;

	@Autowired
	public TbTaskLogServiceImpl(ITbTaskLogDao tbTaskLogDao){
		 this.tbTaskLogDao = tbTaskLogDao;
	}

	@Transactional
	@Override
	public TaskLogVo addTaskLog(TaskLogDto taskLogDto){
        TbTaskLog taskLog = new TbTaskLog();
        taskLog.setTaskId(taskLogDto.getTaskId());
        taskLog.setLogContent(taskLogDto.getLogContent());
        taskLog.setLogType(MsgEnums.TaskLogType.REMARK.getCode());
        taskLog.setLogTime(new Date());
        taskLog.setLogUserId(taskLog.getLogUserId());
        taskLog.setLogUserName(taskLog.getLogUserName());
        this.tbTaskLogDao.save(taskLog);
		return this.buildVo(taskLog);
	}

	private TaskLogVo buildVo(TbTaskLog taskLog){
        TaskLogVo logVo = new TaskLogVo();
        BeanUtils.copyProperties(taskLog, logVo);
        return logVo;
    }

    @Transactional
    @Override
    public void addCreateLog(TbTask task) {
	    String content = task.getCreateUserName()+"指派["+task.getDealName()+"]";
        this.addLog(task, content, MsgEnums.TaskLogType.CREATE.getCode());
    }

    @Transactional
    @Override
    public void addUpdateLog(TbTask oldTask, TbTask task) {
        StringBuilder stringBuilder = new StringBuilder();
        if (!oldTask.getTaskTitle().equals(task.getTaskTitle())){
            stringBuilder.append("标题：").append(oldTask.getTaskTitle()).append("改为").append(task.getTaskTitle());
        }
        if (!oldTask.getTaskContent().equals(task.getTaskContent())){
            stringBuilder.append("内容：").append(oldTask.getTaskContent()).append("改为").append(task.getTaskContent());
        }
        if (!task.getDealId().equals(oldTask.getDealId())){
            stringBuilder.append("指派：[").append(oldTask.getDealName()).append("]改为").append(task.getDealName());
        }
        this.addLog(task, stringBuilder.toString(), MsgEnums.TaskLogType.UPDATE.getCode());
    }

    @Transactional
    @Override
	public void addFinishLog(TbTask task, String remark) {
        this.addLog(task, remark, MsgEnums.TaskLogType.FINISH.getCode());
	}
    @Transactional
	@Override
	public void addAssignLog(TbTask task, String remark) {
        String content = MsgEnums.TaskLogType.ASSIGN.getName()+"给："+task.getDealName();
        if (StringUtils.isNotBlank(remark)) content = content + " 备注："+remark;
        this.addLog(task, content, MsgEnums.TaskLogType.ASSIGN.getCode());
	}
    @Transactional
    @Override
    public void addRefuseLog(TbTask task, String remark) {
        this.addLog(task, remark, MsgEnums.TaskLogType.REFUSE.getCode());
    }
    @Transactional
    @Override
    public void addCloseLog(TbTask task, String remark) {
        this.addLog(task, remark, MsgEnums.TaskLogType.CLOSE.getCode());
    }
    @Transactional
    @Override
    public void addOpenLog(TbTask task, String remark) {
        this.addLog(task, remark, MsgEnums.TaskLogType.OPEN.getCode());
    }

    private void addLog(TbTask task, String remark, String type){
        TbTaskLog taskLog = new TbTaskLog();
        taskLog.setTaskId(task.getId());
        taskLog.setLogType(type);
        taskLog.setLogContent(remark);
        taskLog.setLogTime(new Date());
        taskLog.setLogUserId(task.getLastUpdateId());
        taskLog.setLogUserName(task.getLastUpdateName());
        this.tbTaskLogDao.save(taskLog);
    }


    @Override
    public List<TaskLogVo> findAllLogByTaskId(Integer taskId) {
        List<TbTaskLog> taskLogList = this.tbTaskLogDao.findAllByTaskId(taskId);
        if (CollectionUtil.isNotEmpty(taskLogList)){
            List<TaskLogVo> logVoList = new ArrayList<>();
            taskLogList.forEach((item)->logVoList.add(this.buildVo(item)));
            return logVoList;
        }
        return null;
    }


}
